package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicDelLessonController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonRepository lessonRepository;

    @Autowired
    YxyLessonsTopicLessonItemRepository itemRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/lesson/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;

        // 删除课程
        YxyLessonsTopicLesson lesson = lessonRepository.findById(lessonId);
        if (null != lesson) {
            lesson.setStatus(-1);
            lesson.setSync(0);
            lessonRepository.save(lesson);
        }

        // 删除课时
        List<YxyLessonsTopicLessonItem> items = itemRepository.findByLessonId(lessonId);
        for (YxyLessonsTopicLessonItem item : items) {
            item.setStatus(-1);
        }
        itemRepository.save(items);
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "未获取到课程");
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }
}
